$(function () {
    var address=document.location.hostname+':'+document.location.port
    initTable("/admin/get_all_personnel","#all_user")
    initTable("/admin/get_all_attendance","#user_attendances")
    initTable("/admin/get_all_user_device","#user_devices")
    initTable("/admin/get_all_monitor","#monitors")
    initAddUserModal()


    function initTable(dataUrl,id) {
        let url="http://"+address+dataUrl
        axios.get(url).then(function(res){
            console.log(res)
            new Vue({
                el:id,
                data:{
                    collection:res.data
                }
            })
        }).catch(function (reason) {
            console.log(reason)
        })
    }

    function initAddUserModal() {
        new Vue({
            el:"#addUserModal",
            data:{
                id:'',
                name:'',
                password:'',
                section:'',
                post:''
            },
            methods:{
                addBtn:function () {
                    axios.post("http://"+address+"/admin/add_personnel",{
                        "id":this.id,
                        "name":this.name,
                        "password":this.password,
                        "section":this.section,
                        "post":this.post
                    }).then(function (value) {
                        if (value.data.code==1){
                            alert("添加成功")
                            this.id=''
                            this.name=''
                            this.password=''
                            this.section=''
                            this.post=''
                            initTable("/admin/get_all_personnel","#all_user")
                        }else{
                            alert("添加失败")
                        }
                    }).catch(function (reason) {
                            alert("添加失败")
                        })
                }
            }
        })
    }
})
